package com.yenlien.traceyun.business.controller;

import com.yenlien.traceyun.business.entity.Supplier;
import com.yenlien.traceyun.business.entity.SupplierLog;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.business.service.SupplierService;
import com.yenlien.traceyun.webase.controller.BaseController;
import com.yenlien.traceyun.webase.utils.CheckParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by LiuSha on 2020/7/24.
 * 供应商管理
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController extends BaseController {
    @Autowired
    private SupplierService supplierService;

    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody Supplier supplier) {
        supplier.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        return buildPage(supplierService.findByList(supplier));
    }

    @RequestMapping(value = "/findByListLog", method = RequestMethod.POST)
    public Object findByListLog(@RequestBody SupplierLog supplierLog) {
        CheckParams.checkBean(supplierLog, new String[]{"supplierId", "page"});
        return buildPage(supplierService.findByListLog(supplierLog));
    }

    @RequestMapping(value = "/findByAll", method = RequestMethod.GET)
    public Object findByAll() {
        return success(supplierService.findByAll(UserAuthUtil.getLoginUser().getCompanyId()));
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(@RequestBody Supplier supplier) {
        supplier.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        supplierService.save(supplier);
        return success("success");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(@RequestBody Supplier supplier) {
        supplierService.update(supplier);
        return success("success");
    }

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable String id) {
        return success(supplierService.findById(id));
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Object delete(@PathVariable String id) {
        supplierService.delete(id);
        return success("success");
    }

}
